<?php


namespace SidToken\Utils;

use SidToken\Model\QueueModel;
use SidToken\Model\SidMinerProductionLogModel;
use SidToken\Model\SidTransferLogModel;
use SidToken\Model\SidTrxErrModel;

/**
 * 矿池发放
 * Class MiningUtil
 * @package SidToken\Utils
 */
class MiningUtil
{
    function mining($val, $time = 0)
    {
        $dayAmount = $val['day_amount'];
        $uid = $val['uid'];
        $minerId = $val['id'];
        $minerName = $val['name'];
        $minerOrderSn = $val['order_sn'];
        if (!$time) {
            $time = redis("mining_time");
        }

        $MinerProductionLogModel = new SidMinerProductionLogModel();
        //注意：需要先查询log 是否已经发放，避免重复操作
        $count = $MinerProductionLogModel->where(['miner_id' => $minerId, 'uid' => $uid, 'create_time' => $time])->count();
        if ($count > 0) {
            return;
        }
        $userAddress = get_address($uid);
        $MemberWallet = new MemberWalletUtil();
        //记录矿机产币记录
        $reason = $minerName . "[{$minerOrderSn}]产出";

        if (strtotime(date("Ymd")) != $time) {
            $reason = "[补发]" . $reason;
        }
        $txid = $MinerProductionLogModel->createOrder($uid, $minerId, $dayAmount, time() . mt_rand(1000, 9999), $time);
        $MemberWallet->changeSid($uid, 'inc', 'inner transfer', 'inner transfer', $txid, $dayAmount, $reason);
        //向上推广奖励 调用Queue
        $QueueModel = new QueueModel();
        $url = getApiUrl() . "/App/Reward/rewardMining";
        $post = [
            'uid' => $uid,
            'amount' => $dayAmount,
            'miner' => $minerName
        ];
        //推荐奖励
        $QueueModel->addQueue(M(), [], $url, $post);
    }
}